<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NAvatar,
  NButton,
  NCheckbox,
  NCheckboxGroup,
  NForm,
  NFormItem,
  NImage,
  NInput,
  NSpace,
  NSwitch,
  NTag,
  NPopconfirm,
  NLayout,
  NLayoutSider,
  NLayoutContent,
  NTreeSelect, NSelect,NFlex
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import {formatDate, getToken, renderIcon} from '@/utils'
import { useCRUD } from '@/composables'
import api from '@/api'
import TheIcon from '@/components/icon/TheIcon.vue'
import { useUserStore } from '@/store'
const base_path = import.meta.env.VITE_BASE
import default_image from "@/assets/images/mz.jpg"

defineOptions({ name: 'APP版本管理' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const data = reactive({
  CityData: [],
  radioData:[
    {"value":0,"label":"否"},
    {"value":1,"label":"是"}
  ]
})

const {
  modalVisible,
  modalTitle,
  modalAction,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: 'App版本',
  initForm: {},
  doCreate: api.createApp,
  doUpdate: api.updateApp,
  doDelete: api.deleteApp,
  refresh: () => $table.value?.handlerefresh(),
})

const roleOption = ref([])
const deptOption = ref([])

onMounted(() => {
  $table.value?.handleSearch()
  api.getSpaceList().then((res) => (data.CityData = res.data))
})

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },
    {
    title: '平台',
    key: 'platform',
    width: 60,
    align: 'center',
    render(row) {
      return h(NTag, { type: 'primary',size:"small",bordered:false, style: { margin: '2px 3px' } }, { default: () => row.platform_name })
    },
  },{
    title: '版本号',
    key: 'version',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
      fixed: 'left',
  },
  {
    title: '强制升级',
    key: 'force',
    width: 60,
    align: 'center',
    render(row) {
      return h(NTag, { type: 'primary',size:"small",bordered:false, style: { margin: '2px 3px' } }, { default: () => row.force_name })
    },
  },
    {
    title: '提醒',
    key: 'remind',
    width: 60,
    align: 'center',
    render(row) {
      return h(NTag, { type: 'primary',size:"small",bordered:false, style: { margin: '2px 3px' } }, { default: () => row.remind_name })
    },
  },
    {
    title: '更新内容',
    key: 'content',
    width: 200,
    align: 'center',
    ellipsis: { tooltip: true },
  },

  {
    title: '更新时间',
    key: 'updated_at',
    align: 'center',
    width: 80,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
        NButton,
        { size: 'small', type: 'text', ghost: true },
        {
          default: () => (row.updated_at ? formatDate(row.updated_at) : null),
          icon: renderIcon('mdi:update', { size: 16 }),
        }
      )
    },
  },
  {
    title: '操作',
    key: 'actions',
    width: 120,
    align: 'center',
    fixed: 'right',
    render(row) {
      return [
        withDirectives(
          h(
            NButton,
            {
              size: 'small',
              type: 'primary',
              style: 'margin-right: 8px;',
              onClick: () => {
                handleEdit(row)
              },
            },
            {
              default: () => '编辑',
              icon: renderIcon('material-symbols:edit', { size: 16 }),
            }
          ),
          []
        ),

      ]
    },
  },
]



const validateAddUser = {
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: ['input', 'blur'],
    },
  ],

}
</script>

<template>
  <NLayout has-sider wh-full>
    <NLayoutContent style="background: transparent;margin-left: 20px;">
      <CommonPage show-footer title="App版本管理">

        <!-- 表格 -->
        <CrudTable
          ref="$table"
          v-model:query-items="queryItems"
          :columns="columns"
          :get-data="api.getApp"
           :scroll-x="1200"
          :maxheight="300"
        >
          <template #queryBar>
            <QueryBarItem label="App版本号" :label-width="80">
              <NInput
                v-model:value="queryItems.version"
                clearable
                type="text"
                placeholder="请输入App版本号"
                @keypress.enter="$table?.handleSearch()"
              />
            </QueryBarItem>

          </template>
        </CrudTable>

        <!-- 新增/编辑 弹窗 -->
        <CrudModal
          v-model:visible="modalVisible"
          :title="modalTitle"
          :loading="modalLoading"
          @save="handleSave"
        >
          <NForm
            ref="modalFormRef"
            label-placement="left"
            label-align="left"
            :label-width="80"
            :model="modalForm"
            :rules="validateAddUser"
          >

            <NFormItem label="版本编号" path="version">
              <NInput v-model:value="modalForm.version" clearable placeholder="请输入版本号" />
            </NFormItem>
            <NFormItem label="强制升级" path="force">
              <n-radio-group v-model:value="modalForm.force" name="radiogroup">
                <n-space>
                  <n-radio v-for="item in data.radioData" :key="item.value" :value="item.value">
                    {{ item.label }}
                  </n-radio>
                </n-space>
              </n-radio-group>
            </NFormItem>
            <NFormItem label="提醒" path="remind">
              <n-radio-group v-model:value="modalForm.remind" name="radiogroup">
                <n-space>
                  <n-radio v-for="item in data.radioData" :key="item.value" :value="item.value">
                    {{ item.label }}
                  </n-radio>
                </n-space>
              </n-radio-group>
            </NFormItem>
            <NFormItem label="下载链接" path="url">
              <NInput v-model:value="modalForm.url" clearable placeholder="请输入下载链接" />
            </NFormItem>
            <NFormItem label="更新内容" path="content">
              <NInput type="textarea" v-model:value="modalForm.content" clearable placeholder="请输入更新内容" />
            </NFormItem>

          </NForm>
        </CrudModal>
      </CommonPage>
    </NLayoutContent>
  </NLayout>
  <!-- 业务页面 -->
</template>
